package com.wn.quality.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.common.PageBean;
import com.wn.entity.ProductInspection;
import com.wn.quality.mapper.InventoryBookkeepingMapper;
import com.wn.quality.mapper.ProductInspectionMapper;
import com.wn.quality.service.ProductInspectionService;
import com.wn.utils.PageBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wuqingting
 * @date 2022/10/24
 */
@Service
public class ProductInspectionServiceImpl implements ProductInspectionService {
    @Autowired
    private ProductInspectionMapper productInspectionMapper;
    @Autowired
    private InventoryBookkeepingMapper inventoryBookkeepingMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ProductInspection productInspection) {
        productInspectionMapper.add(productInspection);
        productInspection.getInventoryBookkeeping().setState("检验中");
        inventoryBookkeepingMapper.update(productInspection.getInventoryBookkeeping());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void complete(ProductInspection productInspection) {
        productInspectionMapper.update(productInspection);
        inventoryBookkeepingMapper.update(productInspection.getInventoryBookkeeping());
    }

    @Override
    public ProductInspection getById(int id) {
        return productInspectionMapper.getById(id);
    }

    @Override
    public PageBean<ProductInspection> getPageByCondition(ProductInspection productInspection,int currPage,int pageSize) {
        Page<Object> page = PageHelper.startPage(currPage, pageSize);
        List<ProductInspection> list = productInspectionMapper.getByCondition(productInspection);
        PageBean<ProductInspection> pageBean = PageBeanUtil.getPageBean(list, page, currPage, pageSize);

        return pageBean;
    }

    @Override
    public List<ProductInspection> getAllByCondition(ProductInspection productInspection) {
        return productInspectionMapper.getByCondition(productInspection);
    }
}
